package com.china08.yxyapi.api.comm;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

@RestController
public class LectureResourceUpdateIsYxViewController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @Value("${spring.profiles.active}")
    String profiles;

    @RequestMapping(value = "/api_yw/comm/lecture/resource/update/isyxview", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String find(@RequestBody ReqModel model) {

        // Thread thread = new Thread(new Runnable() {
        // @Override
        // public void run() {
        List<SynModel> list = model.synModels;
        for (SynModel synModel : list) {
            String userId = synModel.userId;
            String phaseId = synModel.phaseId;
            String subjectId = synModel.subjectId;
            String versionId = synModel.versionId;
            List<String> semester = synModel.semesterId;
            if (CollectionUtils.isEmpty(semester)) {
                List<YxyLecturePrePack> packs = yxyLecturePrePackRepository
                        .findByUserIdAndPhaseIdAndSubjectIdAndVersionIdAndShareAndIsDeleteNot(userId, phaseId,
                                subjectId, versionId, 1, 1);
                updateResource(packs);
            } else {
                List<YxyLecturePrePack> packs = yxyLecturePrePackRepository
                        .findByUserIdAndPhaseIdAndSubjectIdAndVersionIdAndSemesterIdInAndShareAndIsDeleteNot(userId,
                                phaseId, subjectId, versionId, semester, 1, 1);
                updateResource(packs);
            }
        }
        // SendEmailUtil.sendEmailValidate("1666722806@qq.com", null,
        // "备课包资源操作成功" + profiles, profiles);
        // }
        // });
        // thread.start();
        return "OK";

    }

    public void updateResource(List<YxyLecturePrePack> packs) {
        for (YxyLecturePrePack yxyLecturePrePack : packs) {
            String userId = yxyLecturePrePack.getUserId();
            List<Res> resources = yxyLecturePrePack.getResources();
            for (Res res : resources) {
                String resourceId = res.getResourceId();
                YxyResResource yxyResResource = yxyResResourceRepository.findById(resourceId);
                if (yxyResResource == null) {
                    continue;
                }
                if (!StringUtils.equals(userId, yxyResResource.getUploaderId())) {
                    continue;
                }
                if (yxyResResource.getViewScope() == 0) {
                    // 如果资源是本人的并且是私有的就把资源共享到区域并且在云校显示，如果是课件或者是教案就加精资源，并自动审核通过
                    yxyResResource.setViewScope(2);
                    yxyResResource.setIsYxView(1);
                    yxyResResource.setStatus(1);
                    String typeId = yxyResResource.getTypeId();
                    if (StringUtils.equals(typeId, "jl01") || StringUtils.equals(typeId, "jl02")) {
                        yxyResResource.setIsHighQualityYx(1);
                    }
                    yxyResResourceRepository.save(yxyResResource);
                }
            }
        }
    }

    protected static class ReqModel {
        public List<SynModel> synModels;
    }

    protected static class SynModel {
        public String userId;
        public String phaseId;
        public String subjectId;
        public String versionId;
        public List<String> semesterId;
    }
}
